    program add_crsp
    character*6 tick(350000),ticker,ftick(6000)
    character*8 cusip(350000),fcusip(6000),tcusp,cusp
    character*9 retrn(350000),price(350000)
    integer(kind=8) nshrs,jvalue,ivol(350000)
      dimension iperm(350000),idate(350000),ishrout(350000),ibg(6000),vwret(350000)
      open(unit=10,file='e:\FIS\crsp_stock_rets_2014.prn')
!      open(unit=11,file='e:\FIS\sorted_fees_202310_202312.txt')
      open(unit=11,file='e:\FIS\sorted_fees_201401_201403_rep.txt')
!      open(unit=99,file='e:\FIS_covid\crsp_sorted_fees_202007_202009.txt')
      open(unit=99,file='e:\FIS\crsp_sorted_fees_201401_201403_rep.txt')
      np=0
      lperm=-1
      i=0
1     i=i+1
2     read(10,1000,end=10)iperm(i),idate(i),tick(i),tcusp,price(i),ivol(i),retrn(i),ishrout(i),vwret(i)
!      write(6,1000)iperm(i),idate(i),tick(i),tcusp,price(i),ivol(i),retrn(i),ishrout(i),vwret(i)
!1000  format(1x,i5,1x,i8,1x,a6,1x,a8,1x,f9.2,1x,i10,1x,a9,1x,i9,1x,f9.6)  
1000  format(1x,i5,1x,i8,1x,a6,1x,a8,1x,a9,1x,i10,1x,a9,1x,i9,1x,f9.6)  
      if(idate(i).lt.20140100.or.idate(i).gt.20140331) go to 2
      if(retrn(i).eq.'       B ') go to 2
      if(retrn(i).eq.'       C ') go to 2
      if(tcusp(1:1).eq.' ') tcusp(1:1)='0'
      if(tcusp(2:2).eq.' ') tcusp(2:2)='0'
      if(tcusp(3:3).eq.' ') tcusp(3:3)='0'
      cusip(i)=tcusp
      if(iperm(i).eq.lperm) go to 1
      np=np+1
      fcusip(np)=cusip(i)
      ftick(np)=tick(i)
      ibg(np)=i
      lperm=iperm(i)
      write(6,6000)np,fcusip(np),ftick(np)
6000  format(1x,i4,1x,a8,1x,a6)      
      go to 1
10    icnt=i-1
      write(6,6001)icnt
6001  format(1x,i6)
      j=1
      tcusp='       1'
100   read(11,1100,end=900)jast,jdate,cusp,ticker,itickt,nshrs,jvalue,age,favg,fmax,fmin,fstd      
!      write(6,1100)jast,jdate,cusp,ticker,itickt,nshrs,jvalue,age,favg,fmax,fmin,fstd      
1100  format(2(1x,i8),1x,a8,2x,a6,4x,i9,2(1x,i14),5(1x,f11.4))      
      if(cusp.eq.tcusp) go to 120
      tcusp=cusp
      j=0
105   j=j+1
      if(j.gt.np) go to 110
      if(cusp.eq.fcusip(j)) go to 125
      go to 105
110   j=0
115   j=j+1
      if(j.gt.np) go to 100
      if(ticker.eq.ftick(j)) go to 125
      go to 115
      
120   if(j.gt.np) go to 100 
125   ii=ibg(j)-1
130   ii=ii+1
      if(cusp.ne.cusip(ii)) go to 100
      if(jdate.ne.idate(ii)) go to 130
!      write(6,6666)ii,cusp,cusip(ii),jdate,idate(ii) 
!      write(99,6666)ii,cusp,cusip(ii),jdate,idate(ii) 
!6666  format(1x,i8,11x,a8,1x,a8,2(1x,i8))      
      write(99,9900)jast,jdate,cusp,ticker,itickt,nshrs,jvalue,age,favg,fmax,fmin,fstd,iperm(ii),idate(ii),cusip(ii),price(ii),ivol(ii),retrn(ii),ishrout(ii),vwret(ii)      
      write(6,9900)jast,jdate,cusp,ticker,itickt,nshrs,jvalue,age,favg,fmax,fmin,fstd,iperm(ii),idate(ii),cusip(ii),price(ii),ivol(ii),retrn(ii),ishrout(ii),vwret(ii)      
!9900  format(2(1x,i8),1x,a8,2x,a6,4x,i9,2(1x,i14),5(1x,f11.4),1x,i5,1x,i8,1x,a8,1x,f9.2,1x,i10,1x,a9,1x,i9,1x,f9.6)   
9900  format(2(1x,i8),1x,a8,2x,a6,4x,i9,2(1x,i14),5(1x,f11.4),1x,i5,1x,i8,1x,a8,1x,a9,1x,i10,1x,a9,1x,i9,1x,f9.6)   
      go to 100
      
900   continue             
      stop

    end program add_crsp
